/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.web.util;

import java.math.BigDecimal;
import java.sql.Timestamp;


/**
 *
 * @author tengyu
 */
public class CommonWebUtil {

    public static int getIntParameter(String value, int defaultValue) {
        if (value != null && !"".equals(value)) {
            try {
                return Integer.parseInt(value);
            } catch (Exception e) {
                return defaultValue;
            }
        } else {
            return defaultValue;
        }
    }

    public static long getLongParameter(String value, long defaultValue) {
        if (value != null && !"".equals(value)) {
            try {
                return Long.parseLong(value);
            } catch (Exception e) {
                return defaultValue;
            }
        } else {
            return defaultValue;
        }
    }

    public static double getDoubleParameter(String value, double defaultValue) {
        if (value != null && !"".equals(value)) {
            try {
                return Double.parseDouble(value);
            } catch (Exception e) {
                return defaultValue;
            }
        } else {
            return defaultValue;
        }
    }
    
    public static BigDecimal getBigDecimalParameter(String value, double defaultValue) {
        BigDecimal bd;
        bd = new BigDecimal(defaultValue);
        if (value != null && !"".equals(value)) {
            try {
                bd =new BigDecimal(value);
                return bd;
            } catch (Exception e) {
                return bd;
            }
        } else {
            return bd;
        }        
    }

    public static String getStringParameter(String value, String defaultValue) {
        if (value != null && !"".equals(value)) {
            return value;
        } else {
            return defaultValue;
        }
    }
    public static Timestamp getTimestampParamter(String value) {
        if (value != null && !"".equals(value)) {
            try {
                if (value.length() == 10) {
                    return Timestamp.valueOf(value + " 00:00:00.0");
                } else {
                    return Timestamp.valueOf(value);
                }
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static boolean validConditionParam(String param) {
        if (param != null && !"".equals(param.trim()) && !"-1".equals(param.trim())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isValidParam(String param) {
        if (param != null && !"".equals(param.trim())) {
            return true;
        } else {
            return false;
        }
    }

    public static String getEqualConditionStr(String paramName, String paramValue) {
        if (validConditionParam(paramValue)) {
            return " and " + paramName + "=" + paramValue;
        } else {
            return "";
        }
    }

    public static String getRangeConditionStr(String range) {
        if (validConditionParam(range)) {
            if ("0".equals(range)) {
                return " and orgTypeId<100";
            } else {
                return " and orgTypeId>=100";
            }
        } else {
            return "";
        }
    }
}
